/* Most of this code comes directly from Autor, Houseman, and Kerr */
/* https://www.journals.uchicago.edu/doi/suppl/10.1086/687522 */

#delimit ;
set more off;
set mat 11000;
macro drop _all;

capture log close;

do chivqr;
/*OLS and IV Estimates of the Effect of Work First job placements on earnings and employment quarters 2-8;
*/;

clear;


gzuse ddata_v9;

gen distyr = distr*10 + partyr;
gen contyr = 10*contr + partyr;

gen regern2_8=(regern2_4+regern5_8);
gen tmpern2_8=(tmpern2_4+tmpern5_8);

quietly {;
  foreach v in wageadj2_4 regern2_4 tmpern2_4  {;
    replace `v'=`v'/3;
  };
   foreach v in wageadj5_8 regern5_8 tmpern5_8  {;
    replace `v'=`v'/4;
  };
   foreach v in wageadj2_8 regern2_8 tmpern2_8 js7_tern new_ear2_8 {;
    replace `v'=`v'/7;
  };
   foreach v in wageadjh1_8 qtremph1_8 qtrtemph1_8 tmpernh1_8 {;
    replace `v'=`v'/8;
  };
};

recode js7_tern .=0;
recode new_ear2_8 .=0;

* Drop the contractor-year cells where tercile equality dropped;

drop if contyr==173|contyr==323|contyr==180|contyr==143|contyr==163|contyr==251|contyr==282|contyr==220|contyr==330|contyr==131|contyr==312|contyr==193|contyr==211;
drop if contyr==60|contyr==23|contyr==92|contyr==13|contyr==40|contyr==51|contyr==100;

*log using ..\Output\final_tables_for_jole_123.log, replace;

* Table 1;
sum age female bl_race wh_race oth_race wageadjh1_8 qtremph1_8;
sum age female bl_race wh_race oth_race wageadjh1_8 qtremph1_8 if emp==0;
sum age female bl_race wh_race oth_race wageadjh1_8 qtremph1_8 if regemp==1;
sum age female bl_race wh_race oth_race wageadjh1_8 qtremph1_8 if tempemp==1;

* Table 2;
sort wageadj2_8;
gen n=_n;
gen double centile=100*n/30522;
replace centile=round(centile,1)/100;
sum centile;
gen pct_direct=regern2_8/wageadj2_8;
foreach c in 0.15 0.25 0.50 0.75 0.85 {;
sum wageadj2_8 pct_direct if centile==`c';
};

drop n centile pct_direct;
preserve;
keep if emp==0;
sort wageadj2_8;
gen n=_n;
gen double centile=100*n/16177;
replace centile=round(centile,1)/100;
sum centile;
gen pct_direct=regern2_8/wageadj2_8;
foreach c in 0.15 0.25 0.50 0.75 0.85 {;
sum wageadj2_8 pct_direct if centile==`c';
};
restore;

preserve;
keep if regemp==1;
sort wageadj2_8;
gen n=_n;
gen double centile=100*n/11583;
replace centile=round(centile,1)/100;
sum centile;
gen pct_direct=regern2_8/wageadj2_8;
foreach c in 0.15 0.25 0.50 0.75 0.85 {;
sum wageadj2_8 pct_direct if centile==`c';
};
restore;

preserve;
keep if tempemp==1;
sort wageadj2_8;
gen n=_n;
gen double centile=100*n/2762;
replace centile=round(centile,1)/100;
sum centile;
gen pct_direct=regern2_8/wageadj2_8;
foreach c in 0.15 0.25 0.50 0.75 0.85 {;
sum wageadj2_8 pct_direct if centile==`c';
};
restore;

* Table 3: See separate do-file;

summ distyr contyr;
xi i.contyr i.distyr;

*log close;

local olsvar age age2 female wh_race oth_race wageadjh1_8 qtremph1_8 yq20001 yq20002 yq20003 yq20004 
             yq20011 yq20012 yq20013 yq20014 yq20021 yq20022 yq20023 yq20024 yq20031;

/* demean all OLS RHS variables */;
/*
quietly foreach v in `olsvar' {;
  egen m_`v'=mean(`v') if emp==0;
  egen mm_`v'=mean(m_`v');
  replace `v'=`v'-mm_`v';
  drop m_`v' mm_`v';
  };

quietly foreach v of varlist _Idistyr_11-_Idistyr_123 {;
  egen m_`v'=mean(`v') if emp==0;
  egen mm_`v'=mean(m_`v');
  replace `v'=`v'-mm_`v';
  drop m_`v' mm_`v';
  };
*/
* 3-Way;

areg tempemp `olsvar' _Idistyr*, absorb(contyr);
predict ert, dresid;
egen meanert = mean(ert), by(contyr);
rename meanert temp_pct;

areg regemp `olsvar' _Idistyr*, absorb(contyr);
predict err, dresid;
egen meanerr = mean(err), by(contyr);
rename meanerr reg_pct;

areg emp `olsvar' _Idistyr*, absorb(contyr);
predict eer, dresid;
egen meaneer = mean(eer), by(contyr);
rename meaneer emp_pct;

*log using ..\Output\final_tables_for_jole.log, replace;

* Wageadj2_8;
* Table 4;
reg wageadj2_8 `olsvar' emp _Idistyr*, cluster(contr);
ivreg wageadj2_8 `olsvar' _Idistyr* (emp = emp_pct), cluster(contr);

* Table 5;
reg wageadj2_8 `olsvar' tempemp regemp _Idistyr*, cluster(contr);
_rmcoll `olsvar' _Idistyr* ;
local cont `r(varlist)';

ivreg wageadj2_8 `olsvar' _Idistyr* (tempemp regemp = temp_pct reg_pct), cluster(contr);
test tempemp=regemp;


sum `cont';
drop _Idistyr_121 _Idistyr_122 _Idistyr_123;


/* Start of New Code */


for num 1/2: gen bX=.;
for num 1/2: gen hX=.;
for num 1/2: gen lX=.;



foreach num of numlist 30(10)90 {;	
	quan wageadj2_8, end1(tempemp) end2(regemp) prone(`olsvar' _Idistyr*) instr(temp_pct reg_pct) tau(`num') ;

	for num 1/2: replace bX=e(bX) if nnn==`num';
	for num 1/2: replace hX=e(highX) if nnn==`num';
	for num 1/2: replace lX=e(lowX) if nnn==`num';

	
	preserve;
	keep if nnn==`num';
	keep b1 b2 l1 l2 h1 h2 nnn;
	if (`num'==30) {;
		save results_ivqr, replace;
	};
	else {;
		append using results_ivqr;
		save results_ivqr, replace;
	};


	restore;
};

